package flex.cc.bdmap.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import flex.cc.PublicUtil;
import flex.cc.bdmap.dao.MasterpointsDao;
import flex.cc.bdmap.entity.MasterpointsEntity;
import flex.cc.bdmap.service.MasterpointsService;
import flex.cc.bdmap.vo.BDMapVo;
import flex.cc.bdmap.vo.MasterpointsVo;
import flex.cc.bdmap.vo.pointsVo;
import flex.cc.order.manage.dao.OrderServiceRescueDao;
import flex.cc.order.manage.dto.order.OrderServiceRescueEntity;
import flex.cc.vehicle_svr.service.MasterpointsToUpService;


@Service("masterpointsService")
public class MasterpointsServiceImpl extends ServiceImpl<MasterpointsDao, MasterpointsEntity> implements MasterpointsService {
	
    private static final Logger logger = LoggerFactory.getLogger(MasterpointsServiceImpl.class);
	
	@Autowired
	private MasterpointsDao masterpointsDao;
	
	@Autowired
	private OrderServiceRescueDao orderServiceRescueDao;
	
	@Autowired
	private MasterpointsToUpService masterpointsToUpService;
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	@Override
	@Transactional
	public Map<String, String> sendMasterPoints(MasterpointsVo masterpointsVo) {
		Map<String, String> resultMap = new HashMap<>();
		String orderNo = masterpointsVo.getOrderNo();
		if(redisTemplate.hasKey(orderNo)){
			resultMap.put("code", "1");
			resultMap.put("msg", "救援单号不存在");
			return resultMap;
		}
		try {
			if(PublicUtil.isNotEmpty(masterpointsVo.getPoints())) {
				// 师傅坐标异步回写上游
				logger.info("vehicleSvr/sendMasterPoints 调用师傅坐标回写上游异步任务-开始:orderNo={}", orderNo);
				masterpointsToUpService.sendMasterpointsToUp(masterpointsVo);
				logger.info("vehicleSvr/sendMasterPoints 调用师傅坐标回写上游异步任务-结束:orderNo={}", orderNo);
			}
		}catch(Exception e) {
			resultMap.put("code", "1");
			resultMap.put("msg", "业务处理异常");
			return resultMap;
		}
		resultMap.put("code", "0");
		resultMap.put("msg", "成功");
		return resultMap;
	}

	@Override
	public BDMapVo getMapInfoByOrderNo(BDMapVo vo) {
		Map<String, Object> param = new HashMap<String, Object>();
        param.put("orderNo", vo.getOrderNo());
        OrderServiceRescueEntity orderServiceRescueEntity = orderServiceRescueDao.getOneByParams(param);
		BDMapVo reponseVo=new BDMapVo();
		if(vo.getGzlongitude()==0.0) {//故障点经度为空(查询经纬度)
	        if(PublicUtil.isNotEmpty(orderServiceRescueEntity)){
	        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getArriveX())) {
	        		reponseVo.setGzlongitude(orderServiceRescueEntity.getArriveX());
	        	}
	        }
		}else {
			reponseVo.setGzlongitude(vo.getGzlongitude());
		}
		if(vo.getGzlatitude()==0.0) {//故障点维度为空(查询经纬度)
			if(PublicUtil.isNotEmpty(orderServiceRescueEntity)){
	        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getArriveY())) {
	        		reponseVo.setGzlatitude(orderServiceRescueEntity.getArriveY());
	        	}
	        }
		}else {
			reponseVo.setGzlatitude(vo.getGzlatitude());
		}
		Map<String, Object> param1 = new HashMap<String, Object>();
        param1.put("orderNo", vo.getOrderNo());
        MasterpointsEntity masterpointsEntity=masterpointsDao.getOneByParams(param1);
        if(PublicUtil.isNotEmpty(masterpointsEntity)) {
        	if(PublicUtil.isNotEmpty(masterpointsEntity.getLongitude())) {
        		reponseVo.setJylongitude(masterpointsEntity.getLongitude());
        	}else {
        		if(PublicUtil.isNotEmpty(orderServiceRescueEntity)){
    	        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getSendX())) {
    	        		reponseVo.setJylongitude(orderServiceRescueEntity.getSendX());
    	        	}
    	        }
        	}
        	if(PublicUtil.isNotEmpty(masterpointsEntity.getLatitude())) {
        		reponseVo.setJylatitude(masterpointsEntity.getLatitude());
        	}else {
        		if(PublicUtil.isNotEmpty(orderServiceRescueEntity)){
        			if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getSendY())) {
    	        		reponseVo.setJylatitude(orderServiceRescueEntity.getSendY());
    	        	}
    	        }
        	}
        }else {
        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity)){
	        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getSendX())) {
	        		reponseVo.setJylongitude(orderServiceRescueEntity.getSendX());
	        	}
	        	if(PublicUtil.isNotEmpty(orderServiceRescueEntity.getSendY())) {
	        		reponseVo.setJylatitude(orderServiceRescueEntity.getSendY());
	        	}
	        }
        }
		reponseVo.setOrderNo(vo.getOrderNo());
		return  reponseVo;
	}


}